package com.zjxf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.PmsAsProduct;
import com.zjxf.base.bean.po.PmsAsProductClassify;
import com.zjxf.base.bean.po.UmsStoreSeller;
import com.zjxf.base.bean.ro.RoPmsAsProduct;
import com.zjxf.base.bean.vo.VoPmsAsProduct;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.repository.PmsAsProductClassifyRepository;
import com.zjxf.repository.PmsAsProductRepository;
import com.zjxf.service.PmsAsProductService;
import com.zjxf.utils.Conversion;
import com.zjxf.utils.DynamicSpecifications;
import com.zjxf.utils.PageUtils;
import com.zjxf.utils.SearchFilter;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 农副产品
 *
 * @author ZhangWei
 * @date 2021/4/6/006 15:58
 */
@Service
@AllArgsConstructor
@Transactional(rollbackOn = RuntimeException.class)
public class PmsAsProductServiceImpl implements PmsAsProductService {

    private final PmsAsProductRepository pmsAsProductRepository;

    private final PmsAsProductClassifyRepository pmsAsProductClassifyRepository;

    public Optional<PmsAsProduct> findById(Long id) {
        return pmsAsProductRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    public Optional<PmsAsProductClassify> findPmsAsProductClassifyById(Long id) {
        return pmsAsProductClassifyRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    @Override
    public void addOrUpdate(VoPmsAsProduct.addOrUpdate vo, UmsStoreSeller umsStoreSeller) {
        Optional<PmsAsProduct> pmsAsProductOptional = findById(vo.getId());
        PmsAsProduct pmsAsProduct;
        if (pmsAsProductOptional.isPresent()) {
            pmsAsProduct = pmsAsProductOptional.get();
            pmsAsProduct = Conversion.voTurnPo(vo, pmsAsProduct);
            pmsAsProduct.setPublisherId(umsStoreSeller.getId());
            pmsAsProduct.setVerifyStatus(SysConst.VerifyStatus.IN_REVIEW.getCode());
        } else {
            pmsAsProduct = new PmsAsProduct();
            pmsAsProduct = Conversion.voTurnPo(vo, pmsAsProduct);
            pmsAsProduct.setPublisherId(umsStoreSeller.getId());
        }
        pmsAsProductRepository.save(pmsAsProduct);
    }

    @Override
    public void delete(Long id) throws OperationException {
        PmsAsProduct pmsAsProduct = findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
        pmsAsProduct.setDeleteState(SysConst.DeleteState.DELETE_STATE.getCode());
        pmsAsProductRepository.save(pmsAsProduct);
    }

    @Override
    public PmsAsProduct getEntity(Long id) throws OperationException {
        return findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
    }

    @Override
    public RoPmsAsProduct.Details getEntityRo(PmsAsProduct pmsAsProduct) {
        return Conversion.poTurnRo(pmsAsProduct, new RoPmsAsProduct.Details());
    }

    @Override
    public Page<PmsAsProduct> page(VoPmsAsProduct.filter filter) {
        List<SearchFilter> searchFilterList = new ArrayList<>();
        if (filter.getKeywords() != null) {
            searchFilterList.add(new SearchFilter("storeId", SearchFilter.Operator.EQ, filter.getStoreId()));
        }
        if (StringUtils.isNotBlank(filter.getKeywords())) {
            searchFilterList.add(new SearchFilter("title", SearchFilter.Operator.LIKE, filter.getKeywords()));
        }
        if (filter.getVerifyStatus() != null) {
            searchFilterList.add(new SearchFilter("verifyStatus", SearchFilter.Operator.EQ, filter.getVerifyStatus()));
        }
        if (filter.getShowLocation() != null) {
            searchFilterList.add(new SearchFilter("showLocation",SearchFilter.Operator.EQ,filter.getShowLocation()));
        }
        Specification<PmsAsProduct> specification = DynamicSpecifications.bySearchFilterByBasic(searchFilterList, filter.getStartTime(), filter.getEndTime());
        return pmsAsProductRepository.findAll(specification, PageUtils.buildPageRequest(filter.getPageNum(), filter.getPageSize()));
    }

    @Override
    public JSONObject sellerPageRo(Page<PmsAsProduct> pmsAsProductPage) {
        List<PmsAsProduct> pageList = pmsAsProductPage.getContent();
        List<RoPmsAsProduct.SellerPage> roList = pageList.stream().map(item -> {
            RoPmsAsProduct.SellerPage sellerPage = Conversion.poTurnRo(item, new RoPmsAsProduct.SellerPage());
            try {
                PmsAsProductClassify pmsAsProductClassify = findPmsAsProductClassifyById(item.getAsClassifyId()).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
                sellerPage.setClassifyName(pmsAsProductClassify.getName());
            } catch (OperationException e) {
                e.printStackTrace();
            }
            return sellerPage;
        }).collect(Collectors.toList());
        return PageUtils.page(pmsAsProductPage, roList);
    }

    @Override
    public void verify(VoPmsAsProduct.verify verify) throws OperationException {
        if (verify.getVerifyStatus().equals(SysConst.VerifyStatus.NO_PASS_REVIEW.getCode())
                && StringUtils.isBlank(verify.getReason())) {
            throw new OperationException("请输入未通过原因");
        }
        PmsAsProduct pmsAsProduct = findById(verify.getId()).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
        pmsAsProduct = Conversion.voTurnPo(verify, pmsAsProduct);
        pmsAsProductRepository.save(pmsAsProduct);
    }
}
